package util;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class MailUtils {
	public static void sendMail(String userEmail, String emailContent)
			throws AddressException, MessagingException {
		// 1.得到对象 Session，通过邮件服务器发送邮件
		Properties props = new Properties();
		// 设置发送邮件协议
		props.setProperty("mail.transport.protocol", "SMTP");
		// 设置发送邮箱的账号
		props.setProperty("mail.host", "smtp.163.com");
		// 设置SMTP服务器是否需要用户验证，肯定需要验证
		props.setProperty("mail.smtp.auth", "true");
		// 创建验证器
		Authenticator auth = new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				String userName="yxw1542852290";
				String password="LSQBLFGDZXPBJJWY";
				return new PasswordAuthentication(userName,password);
			}
			
		};
		Session session=Session.getInstance(props, auth);
		//2.发送邮件
		Message message=new MimeMessage(session);
		message.setFrom(new InternetAddress("yxw1542852290@163.com"));
		message.setRecipient(RecipientType.TO, new InternetAddress(userEmail));
		message.setSubject("用户激活");
		// message.setText("这是一封激活邮件，请<a href='#'>点击</a>");
		message.setContent(emailContent, "text/html;charset=utf-8");
		// 3.创建 Transport用于将邮件发送
		Transport.send(message);
}
}